var express = require("express");
var Hotel = require("../models/hotel");
var router = express.Router();
const Sequelize = require("sequelize");
const Op = Sequelize.Op;

//获取酒店列表
router.get("/hotels", function (req, res) {
  const body = req.query;
  const size = parseInt(body.pagesize);
  const num = (parseInt(body.pagenum) - 1) * size;
  console.log(body);
  if (body.rank) {
    Hotel.findAndCountAll({
      where: {
        name: {
          // 模糊查询
          [Op.like]: "%" + body.name + "%",
        },
        rank: parseInt(body.rank),
        position: {
          [Op.like]: "%" + body.position + "%",
        },
      },
      offset: num,
      limit: size,
    })
      .then((hotel) => {
        res.json({
          status: 201,
          hotel: hotel.rows,
          total: hotel.count,
        });
      })
      .catch((err) =>
        res.json({
          status: 500,
          msg: "查询失败",
          err: err.message,
        })
      );
    return;
  }
  Hotel.findAndCountAll({
    where: {
      name: {
        // 模糊查询
        [Op.like]: "%" + body.name + "%",
      },
      // rank: parseInt(body.rank),
      position: {
        [Op.like]: "%" + body.position + "%",
      },
    },
    offset: num,
    limit: size,
  })
    .then((hotel) => {
      res.json({
        status: 201,
        hotel: hotel.rows,
        total: hotel.count,
      });
    })
    .catch((err) =>
      res.json({
        status: 500,
        msg: "查询失败",
        err: err.message,
      })
    );
});
//获取单个景区信息
router.get("/hotel", function (req, res) {
  const body = req.query;
  if (body.id == 0) body.id = 8;
  console.log(body);
  Hotel.findOne({
    where: {
      id: body.id,
    },
  })
    .then((hotel) => {
      console.log(hotel);
      res.json({
        status: 201,
        hotel: hotel._previousDataValues,
      });
    })
    .catch((err) =>
      res.json({
        status: 500,
        msg: "查询失败",
        err: err.message,
      })
    );
  return;
});
//删除酒店信息
router.delete("/hotel", function (req, res) {
  const body = req.body;
  Hotel.destroy({
    where: {
      id: body.id,
    },
  }).then(() => {
    res.json({
      status: 201,
    });
  });
});
//添加酒店信息
router.post("/hotel", function (req, res) {
  const body = req.body;
  // console.log(body)
  Hotel.create({
    name: body.name,
    src: body.src,
    position: body.position,
    rank: parseInt(body.rank),
    price: parseInt(body.price),
    traffic: body.traffic,
    tel: parseInt(body.tel),
  }).then(() => {
    res.json({
      status: 201,
    });
  });
});
//修改酒店信息
router.put("/hotel", function (req, res) {
  const body = req.body;
  Hotel.update(
    {
      name: body.name,
      src: body.src,
      position: body.position,
      rank: parseInt(body.rank),
      price: parseInt(body.price),
      traffic: body.traffic,
      tel: parseInt(body.tel),
    },
    {
      where: {
        id: body.id,
      },
    }
  ).then(() => {
    res.json({
      status: 201,
    });
  });
});
//处理剩余的
router.use((err, req, res, next) => {
  //接受 next（err）
  // const err_log = `
  //   错误名：${err.name}
  //   错误消息：${err.message}
  //   错误堆栈：${err.stack}
  //   错误时间：${new Date()}`;
  // fs.appendFile("./err_log.txt", err_log, err => {
  //   res.writeHead(500, {});
  //   res.end("500, 服务器繁忙，请稍后重试");
  // });
  res.writeHead(500, {});
  res.end("500, 服务器繁忙，请稍后重试");
});

module.exports = router;
